package com.cyandpos.pad.offline.offlinedb;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;

public class SqlHelper extends SQLiteOpenHelper {

    public SQLiteDatabase sqlite;

    public SqlHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public boolean beginTransaction() {
        if (sqlite != null) {
            sqlite.beginTransaction();
            return true;
        }
        return false;
    }

    public boolean setTransactionSuccessful() {
        if (sqlite != null) {
            sqlite.setTransactionSuccessful();
            return true;
        }
        return false;
    }

    public boolean endTransaction() {
        if (sqlite != null) {
            sqlite.endTransaction();
            return true;
        }
        return false;

    }

    public boolean execSQL(String sql) {
        try {
            sqlite.execSQL(sql);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    boolean execSQL(String sql, String[] values) {
        try {
            sqlite.execSQL(sql, values);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public boolean insert(String tableName, ContentValues values) {
        try {
           long res= sqlite.insert(tableName, null, values);
           if(res==-1) {
               return false;
           }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean update(String tableName, ContentValues values, String whereClause, String[] wherevalues) {
        try {
            sqlite.update(tableName, values, whereClause, wherevalues);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public boolean delete(String tableName, String whereClause, String[] whereArgs) {
        try {
            sqlite.delete(tableName, whereClause, whereArgs);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public Cursor find(String sql, String[] args) {
        Cursor cur = sqlite.rawQuery(sql, args);
        return cur;
    }
    public Cursor find(String tableName, String[] columns, String selection, String[] selectionArgs) {
        Cursor cur = sqlite.query(tableName, columns, selection, selectionArgs, null, null, null);
        return cur;
    }

    public boolean open(String database) {
        sqlite = SQLiteDatabase.openOrCreateDatabase(database, null);

        if (sqlite != null)
            return true;

        return false;
    }

    public boolean isOpen() {
        if (sqlite != null && sqlite.isOpen())
            return true;

        return false;
    }

    @Override
    public void close() {
        if (sqlite != null && sqlite.isOpen())
            sqlite.close();
    }

}
